package cn1shared.ui;

import com.codename1.ui.Command;
import com.codename1.ui.Container;
import com.codename1.ui.Dialog;
import com.codename1.ui.Display;
import com.codename1.ui.Form;
import com.codename1.ui.MenuBar;
import com.codename1.ui.SideMenuBar;
import com.codename1.ui.animations.CommonTransitions;
import com.codename1.ui.events.ActionEvent;
import com.codename1.ui.layouts.BorderLayout;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.util.Resources;

import java.io.IOException;


public abstract class AbstractForm extends Form {

	public static Resources theme;

	private boolean built = false;
	protected final Form parent;

	protected AbstractForm() {
		super();
		parent = null;
		getTitleStyle().setBgTransparency(0x00);
	}

	protected AbstractForm(String title, Form parent) {
		super(title);
		this.parent = parent;
		getTitleStyle().setBgTransparency(0x00);
	}

	protected AbstractForm(String title) {
		this(title, null);
	}

	/** To be overridden */
	protected abstract Container getPanel() throws IOException;

	private void buildUI()
	{
		if (built)
			return;
		try {
			addActions();
			Container panel = getContentPane();
			panel.setUIID("_DefaultBg");
			panel.setScrollable(false);
			panel.setLayout(new BorderLayout());
			FormUtils.noSpacing(panel);
			panel.addComponent(BorderLayout.CENTER, getPanel());
			built = true;
		} catch(IOException e) {
			if (retryDialog())
				buildUI();
			else
				Display.getInstance().exitApplication();
		}
	}

	protected boolean isAndroid() {
		// https://groups.google.com/d/msg/codenameone-discussions/hyLX75moAMk/V-_zyNxADqQJ
		String platformName = Display.getInstance().getPlatformName();
		return platformName.toLowerCase().indexOf("and") == 0;
	}

	protected void addActions()
	{
		if (parent != null) {
			final Command back = new Command(" Back") {
				@Override
				public void actionPerformed(ActionEvent evt) {
					if (canGoBack())
						parent.showBack();
				}
			};
			back.setIcon(theme.getImage("back.png"));
			setBackCommand(back);
		}
	}

	protected void hideTitleArea() {
		// hack
		Container titleArea = getTitleArea();
		titleArea.removeAll();
		FormUtils.setHeight(titleArea, 0);
	}

	protected boolean canGoBack() {
		return true; // to be overridden
	}

	public void show() {
		buildUI();
		super.show();
	}

	public void showBack() {
		buildUI();
		super.showBack();
	}

	protected static void useMenuSidebar() {
		UIManager.getInstance().getLookAndFeel().setMenuBarClass(SideMenuBar.class);
		Display.getInstance().setCommandBehavior(Display.COMMAND_BEHAVIOR_SIDE_NAVIGATION);
	}

	protected static void useMenuTitleBack() {
		UIManager.getInstance().getLookAndFeel().setMenuBarClass(MenuBar.class);
		Display.getInstance().setCommandBehavior(Display.COMMAND_BEHAVIOR_BUTTON_BAR_TITLE_BACK);
	}

	protected void disableInTransition() {
		setTransitionInAnimator(CommonTransitions.createEmpty());
	}

	protected void disableOutTransition() {
		setTransitionOutAnimator(CommonTransitions.createEmpty());
	}

	public static boolean retryDialog() {
		return Dialog.show("Connection Failed", "Unable to connect to the server.", "Retry", "Quit");
	}

	public static abstract class RemoteCommand extends Command {

		public RemoteCommand() {
			super("");
		}
		public RemoteCommand(String command) {
			super(command);
		}

		/** To be overridden */
		protected abstract void call(ActionEvent evt) throws IOException;

		@Override
		public void actionPerformed(ActionEvent evt) {
			try {
				call(evt);
			} catch(IOException e) {
				if (retryDialog())
					actionPerformed(evt);
				else
					Display.getInstance().exitApplication();
			}
		}
	}
}
